﻿using System;
using System.Web;
using System.Web.Caching;
using sBlog.Net.Infrastructure;
using System.Collections.Generic;
using sBlog.Net.Domain.Entities;
using sBlog.Net.Memcache;
using sBlog.Net.Domain;
using sBlog.Net.Domain.Interfaces;

namespace sBlog.Net.Services
{
    public class MemcaheService : IMemcaheService
    {
        private readonly IPost _postRepository;
        private readonly IUser _userRepository;
        private readonly ITag _tagReponsitory;
        private readonly IFollow _followReponsitory;

        public MemcaheService(IPost postRepository, IUser userRepository, ITag tagReponsitory, IFollow followReponsitory)
        {
            _postRepository = postRepository;
            _userRepository = userRepository;
            _tagReponsitory = tagReponsitory;
            _followReponsitory = followReponsitory;
        }

        public List<PostEntity> GetHomePageWithPage(int pageSize, int pageNumber)
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetHomePageWithPage + "_" + pageNumber);
            if (obj == null)
            {
                postEntities = _postRepository.GetHomePageWithPage(pageSize, pageNumber);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetHomePageWithPage + "_" + pageNumber, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }
            return postEntities;
        }

        public PostEntity GetPostByUrl(string url, byte entryType)
        {
            PostEntity post = new PostEntity();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetPostByUrl + "_" + url);
            if (obj == null)
            {
                post = _postRepository.GetPostByUrl(url, entryType);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetPostByUrl + "_" + url, post);
            }
            else
            {
                post = (PostEntity)obj;
            }

            return post;
        }

        public List<PostEntity> GetMostPostByAuthors(int OwnerUserID, int currentPostID)
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetMostPostByAuthors + "_" + OwnerUserID);
            if (obj == null)
            {
                postEntities = _postRepository.GetMostPostByAuthors(OwnerUserID, currentPostID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetMostPostByAuthors + "_" + OwnerUserID, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }
            return postEntities;
        }

        public List<PostEntity> ShowReadContentPage()
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyShowReadContentPage);
            if (obj == null)
            {
                postEntities = _postRepository.ShowReadContentPage();
                MemcachedProvider.Instance().Add(MemCacheKey.KeyShowReadContentPage, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }
            return postEntities;
        }

        public List<PostEntity> GetPostsByUserID(int userID)
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetPostsByUserIDNoNeedentryType + "_" + userID);

            if (obj == null)
            {
                postEntities = _postRepository.GetPostsByUserID(userID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetPostsByUserIDNoNeedentryType + "_" + userID, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }

            return postEntities;
        }

        public List<PostEntity> GetMostViewPostByCatSlug(string catslug)
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetMostViewPostByCatSlug + "_" + catslug);
            if (obj == null)
            {
                postEntities = _postRepository.GetMostViewPostByCatSlug(catslug);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetMostViewPostByCatSlug + "_" + catslug, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }
            return postEntities;
        }

        public int GetTotalViewPostByUserId(int userID)
        {
            int TotalViewPost = 0;
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetTotalViewPostByUserId + "_" + userID);
            if (obj == null)
            {
                TotalViewPost = _postRepository.GetTotalViewPostByUserId(userID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetTotalViewPostByUserId + "_" + userID, TotalViewPost);
            }
            else
            {
                TotalViewPost = (int)obj;
            }
            return TotalViewPost;
        }

        public List<PostEntity> GetPosts(int userID)
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetPostsByUserID);

            if (obj == null)
            {
                postEntities = _postRepository.GetPosts(userID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetPostsByUserID, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }

            return postEntities;
        }

        public List<PostEntity> GetPostsByUserID(int userID, byte entryType)
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetPostsByUserIDNeedentryType + "_" + userID);

            if (obj == null)
            {
                postEntities = _postRepository.GetPostsByUserID(userID, entryType);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetPostsByUserIDNeedentryType + "_" + userID, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }

            return postEntities;
        }

        public List<PostEntity> GetNewPostByAuthor(int ownerUserID)
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetNewPostByAuthor + "_" + ownerUserID);

            if (obj == null)
            {
                postEntities = _postRepository.GetNewPostByAuthor(ownerUserID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetNewPostByAuthor + "_" + ownerUserID, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }
            return postEntities;
        }

        public List<PostEntity> GetPostFeature()
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetPostFeature);

            if (obj == null)
            {
                postEntities = _postRepository.GetPostFeature();
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetPostFeature, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }
            return postEntities;
        }

        public IEnumerable<PostEntity> GetNewPosts()
        {
            IEnumerable<PostEntity> postEntities;
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetNewPosts);
            if (obj == null)
            {
                postEntities = _postRepository.GetNewPosts();
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetNewPosts, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }
            return postEntities;
        }

        /// 

        ///User
        public List<UserEntity> GetAllUsersPromote()
        {
            List<UserEntity> lstUser;
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetAllUsersPromote);

            if (obj == null)
            {
                lstUser = _userRepository.GetAllUsersPromote();
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetAllUsersPromote, lstUser);
            }
            else
            {
                lstUser = (List<UserEntity>)obj;
            }

            return lstUser;
        }

        public UserEntity GetUserObjByUserName(string userName)
        {
            UserEntity user;
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetUserObjByUserName + "_" + userName);
            if (obj == null)
            {
                user = _userRepository.GetUserObjByUserName(userName);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetUserObjByUserName + "_" + userName, user);
            }
            else
            {
                user = (UserEntity)obj;
            }
            return user;
        }

        public IEnumerable<UserEntity> GetListMlogFace()
        {
            IEnumerable<UserEntity> lstUser;
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetListMlogFace);

            if (obj == null)
            {
                lstUser = _userRepository.GetListMlogFace();
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetListMlogFace, lstUser);
            }
            else
            {
                lstUser = (IEnumerable<UserEntity>)obj;
            }

            return lstUser;
        }

        public UserEntity GetUserObjByUserID(int userID)
        {
            UserEntity userEntity = new UserEntity();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetUserObjByUserID + "_" + userID);
            if (obj == null)
            {
                userEntity = _userRepository.GetUserObjByUserID(userID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetUserObjByUserID + "_" + userID, userEntity);
            }
            else
            {
                userEntity = (UserEntity)obj;
            }

            return userEntity;
        }

        public List<PostEntity> GetPages(int userID)
        {
            List<PostEntity> postEntities = new List<PostEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetPages + "_" + userID);
            if (obj == null)
            {
                postEntities = _postRepository.GetPages(userID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetPages + "_" + userID, postEntities);
            }
            else
            {
                postEntities = (List<PostEntity>)obj;
            }

            return postEntities;
        }

        ///

        ///Tag
        public List<TagEntity> MostTagPosts()
        {
            List<TagEntity> model = new List<TagEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyMostTagPosts);
            if (obj == null)
            {
                model = _tagReponsitory.GetMostTags();
                MemcachedProvider.Instance().Add(MemCacheKey.KeyMostTagPosts, model);
            }
            else
            {
                model = (List<TagEntity>)obj;
            }

            return model;
        }

        public List<TagEntity> GetTagsByPostID(int postID)
        {
            var tagEntities = new List<TagEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetTagsByPostID + "_" + postID);
            if (obj == null)
            {
                tagEntities = _tagReponsitory.GetTagsByPostID(postID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetTagsByPostID + "_" + postID, tagEntities);
            }
            else
            {
                tagEntities = (List<TagEntity>)obj;
            }

            return tagEntities;
        }

        ///

        ///Follow
        public List<FollowEntity> GetFollowByFollowingUserID(int followingUserID)
        {
            List<FollowEntity> followEntity = new List<FollowEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetFollowByFollowingUserID + "_" + followingUserID);
            if (obj == null)
            {
                followEntity = _followReponsitory.GetFollowByFollowingUserID(followingUserID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetFollowByFollowingUserID + "_" + followingUserID, followEntity);
            }
            else
            {
                followEntity = (List<FollowEntity>)obj;
            }

            return followEntity;
        }

        public List<FollowEntity> GetFollowByFollowerUserID(int followerUserID)
        {
            List<FollowEntity> followEntity = new List<FollowEntity>();
            object obj = MemcachedProvider.Instance().Get(MemCacheKey.KeyGetFollowByFollowerUserID + "_" + followerUserID);
            if (obj == null)
            {
                followEntity = _followReponsitory.GetFollowByFollowerUserID(followerUserID);
                MemcachedProvider.Instance().Add(MemCacheKey.KeyGetFollowByFollowerUserID + "_" + followerUserID, followEntity);
            }
            else
            {
                followEntity = (List<FollowEntity>)obj;
            }
            return followEntity;
        }
        ///
    }
}
